package com.javastar.common.utils;

import com.javastar.common.enums.TokenEnum;

import javax.servlet.http.HttpServletRequest;

/**
 * 登陆用户解析工具类
 *
 * @author zxx
 * @date 2023/7/25
 */
public class LoginUserParseUtil {
    public static <T> T getLoginUser(Class<T> clazz) {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        if (!ValidatorUtils.isEmpty(request)) {
            Object loginUser = request.getAttribute("loginUser");
            if (!ValidatorUtils.isEmpty(loginUser)) {
                return (T) loginUser;
            }
            String userToken = request.getHeader(TokenEnum.USER_TOKEN.getTokenKey());
            if (!ValidatorUtils.isEmpty(userToken)) {
                if (JwtUtil.validateToken(userToken, TokenEnum.USER_TOKEN)) {
                    return JwtUtil.getBean(userToken, TokenEnum.USER_TOKEN, clazz);
                }
            }
        }
        return null;
    }
}
